<?php
//--------------------------------------------------------------------------------------------------------------------------
// Changed code to solve 'albums spliting'
// 
// function greengar_get_whiteboardalbum_guids()
// function get_whiteboardalbums() changed to greengar_get_whiteboardalbum_entities()
// function greengar_get_whiteboardalbum()
// function greengar_create_whiteboardalbum()
// 
// If you used those functions somewhere else besides where I used them, be sure to modify them

/**
 * Returns an array of Whiteboard album GUIDs, may be filtered by month or owner.
 * 
 * @param array/int $months - Months we want to filtered out. Null means all months
 * @param array/int $owner_guids - Owners we want to filtered out. Null means all owners
 * @return array - Whiteboard album GUIDs
 */
function greengar_get_whiteboardalbum_guids($months = null, $years = null, $owner_guids = null)
{
    $albums = greengar_get_whiteboardalbum_entities($months, $years, $owner_guids);
    
    foreach ($albums as $a)
        $album_guids[] = $a->getGUID();
    
    return $album_guids;
}

/**
 * Return a single Whiteboard album entity. For multiple albums search, use greengar_get_whiteboardalbum_entities()
 * 
 * @param int $owner_guid - Null means current logged in user
 * @param int $month - Null means this month (server's time)
 * @return object/null - A Whiteboard album object or null on failure
 */
/*function greengar_get_whiteboardalbums($owner_guid, $month, $year)
{
    $albums = greengar_get_whiteboardalbum_entities($month, $year, $owner_guid);
        
    if (empty($albums))
        return false;
    
    return $albums;
}*/

/**
 * Returns multiple Whiteboard album entities.
 * 
 * @param array/int $months - Months we want to filtered out. Null means all months
 * @param array/int $owner_guids - Owners we want to filtered out. Null means all owners
 * @return array - The array of Whiteboard album objects
 */
function greengar_get_whiteboardalbum_entities($months = null, $years = null, $owner_guids = null)
{
    if ($months !== null)
    {
        $pairs[] = array('name' => 'month', 'value' => $months, 'operand' => '=');
    }
    
    if ($years !== null)
    {
        $pairs[] = array('name' => 'year', 'value' => $years, 'operand' => '=');
    }   
    
    $pairs[] = array('name' => WHITEBOARD_ALBUM_METADATA, 'value' => 1, 'operand' => '=');
    
    $options = array(
        'metadata_name_value_pairs' => $pairs,
        'metadata_owner_guids' => $owner_guids
    );
    $albums = elgg_get_entities_from_metadata($options);
    
    return $albums;
}

function greengar_count_whiteboardalbum_entities($months = null, $years = null, $owner_guids = null)
{
    if ($months !== null)
    {
        $pairs[] = array('name' => 'month', 'value' => $months, 'operand' => '=');
    }
    
    if ($years !== null)
    {
        $pairs[] = array('name' => 'year', 'value' => $years, 'operand' => '=');
    }   
    
    $pairs[] = array('name' => WHITEBOARD_ALBUM_METADATA, 'value' => 1, 'operand' => '=');
    
    $options = array(
        'metadata_name_value_pairs' => $pairs,
        'metadata_owner_guids' => $owner_guids,
        
        'count' => true
    );
    $albums = elgg_get_entities_from_metadata($options);
    
    return $albums;
}

/**
 * Create this month's Whiteboard album for a user.
 * 
 * @param int $owner_guid - The owner of the album we want to create. Null means current logged in user
 * @return TidypicsAlbum - The TidypicsAlbum 'Whiteboard - <month>, <year>' object
 */
function greengar_create_whiteboardalbum($owner_guid, $month, $year)
{
    $num_albums = greengar_count_whiteboardalbum_entities($month, $year, $owner_guid);
    
    $album = new WhiteboardAlbum();
    $attributes['container_guid'] = $owner_guid;
    $attributes['owner_guid'] = $owner_guid;
    
    $timestamp = mktime(0, 0, 0, $month, 1, $year);
    $month_full = date('F', $timestamp);
    
    $attributes['title'] = WHITEBOARD_ALBUM_TITLE.' - '.$month_full.', '.$year.' - '.($num_albums+1);
    $attributes['description'] = $month_full.', '.$year.' '.WHITEBOARD_ALBUM_DESC.' no. '.($num_albums+1);        
    $metadata['month'] = $month;
    $metadata['year'] = $year;
    
    $album->saveAlbum($attributes, $metadata);
    
    return $album;
}